﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

namespace LuWeibo.Views
{
    public partial class CookieSettingPage : PhoneApplicationPage
    {
        private List<string> ImageSources = new List<string> 
        {
            "/CookieImages/cookie_0.jpg", 
            "/CookieImages/cookie_1.jpg" ,
            "/CookieImages/cookie_2.jpg",
            "/CookieImages/cookie_3.jpg",
        };

        private int selectIndex = 0; //当前正在播放的图片
        private bool Increasing = true;//是否在递增

        public CookieSettingPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            selectIndex = 0;
            SetImage();
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromMilliseconds(2000);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            SetImage();

            //先创建消失动画
            Storyboard goAnimation = CreateImageGoAnimation();
            goAnimation.FillBehavior = FillBehavior.HoldEnd;
            goAnimation.Begin();
            goAnimation.Completed += goAnimation_Completed;
        }

        void goAnimation_Completed(object sender, EventArgs e)
        {
            if (Increasing)
            {
                selectIndex++;
            }
            else
            {
                selectIndex--;
            }
            SetImage();
            //消失动画播放完毕接着播放进入动画
            Storyboard comeAnimation = CreateImageComeAnimation();
            comeAnimation.FillBehavior = FillBehavior.HoldEnd;
            comeAnimation.Begin();
        }

        private void SetImage()
        {
            string imageUrl = ImageSources.ElementAt(selectIndex);
            BitmapImage image = new BitmapImage();
            image.UriSource = new Uri(imageUrl, UriKind.Relative);
            this.CookieImage.Source = image;

            UIElementCollection circles = this.Circles.Children;
            for (int i = 0; i < circles.Count; i++)
            {
                Ellipse e = (Ellipse)circles.ElementAt(i);
                if (i == selectIndex)
                {
                    e.Fill = App.Current.Resources["SunColorBrush"] as SolidColorBrush;
                }
                else
                {
                    e.Fill = App.Current.Resources["SkyColorBrush"] as SolidColorBrush;
                }
            }

            if (selectIndex == ImageSources.Count - 1)
            {
                Increasing = false;
            }
            if (selectIndex == 0)
            {
                Increasing = true;
            }
        }

        private Storyboard CreateImageGoAnimation()
        {
            Storyboard imageGoStoryBoard = new Storyboard();

            DoubleAnimation goAnimation = new DoubleAnimation { From = 1, To = 0, Duration = TimeSpan.FromMilliseconds(250) };
            Storyboard.SetTargetProperty(goAnimation, new PropertyPath("(UIElement.Opacity)"));
            Storyboard.SetTarget(goAnimation, this.CookieImage);
            imageGoStoryBoard.Children.Add(goAnimation);

            goAnimation = new DoubleAnimation { From = 0, To = -455, Duration = TimeSpan.FromMilliseconds(250) };
            Storyboard.SetTargetProperty(goAnimation, new PropertyPath("(Canvas.Left)"));
            Storyboard.SetTarget(goAnimation, this.CookieImage);
            imageGoStoryBoard.Children.Add(goAnimation);

            return imageGoStoryBoard;
        }

        private Storyboard CreateImageComeAnimation()
        {
            Storyboard imageComeStoryBoard = new Storyboard();

            DoubleAnimation comeAnimation = new DoubleAnimation { From = 0, To = 1, Duration = TimeSpan.FromMilliseconds(200) };
            Storyboard.SetTargetProperty(comeAnimation, new PropertyPath("(UIElement.Opacity)"));
            Storyboard.SetTarget(comeAnimation, this.CookieImage);
            imageComeStoryBoard.Children.Add(comeAnimation);

            comeAnimation = new DoubleAnimation { From = 455, To = 0, Duration = TimeSpan.FromMilliseconds(200) };
            Storyboard.SetTargetProperty(comeAnimation, new PropertyPath("(Canvas.Left)"));
            Storyboard.SetTarget(comeAnimation, this.CookieImage);
            imageComeStoryBoard.Children.Add(comeAnimation);

            return imageComeStoryBoard;
        }
    }
}